package com.gy.boot.HuTool;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.extra.tokenizer.Result;
import cn.hutool.extra.tokenizer.TokenizerEngine;
import cn.hutool.extra.tokenizer.TokenizerUtil;
import cn.hutool.extra.tokenizer.Word;
import cn.hutool.http.HttpUtil;
import com.gy.boot.common.util.BlankUtil;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 云图工具
 * */
public class cloudPicture {

    @Test
    public void test() {
        // 1. 爬去关键字;
        List<String> listTitle = new ArrayList<>();
        crawlData(listTitle, 1);
        StringBuilder titleSB = new StringBuilder();
        for (String title : listTitle) {
            titleSB.append(title.trim()).append(";\n");
        }
        System.out.println(titleSB);
        // 2. 组织分词;
        engineWord(titleSB);

        // 3. 生成云图;

    }

    /**
     * 分词处理
     * */
    private void engineWord(CharSequence text) {
        if (BlankUtil.isBlank(text)) {
            return;
        }
        HashMap<String, Integer> wordMap = MapUtil.newHashMap(1000);
        TokenizerEngine engine = TokenizerUtil.createEngine();
        Result result = engine.parse(text);
        for (Word word : result) {
            String str = word.getText();
            if (BlankUtil.isBlank(str)) {
                continue;
            }
            wordMap.merge(str, 1, (a, b) -> a + b);
        }
        Comparator<Map.Entry<String,Integer>> comparator = (m1, m2) -> m2.getValue().compareTo(m1.getValue()) ;
        LinkedHashMap<String, Integer> wordMapLinked = CollUtil.sortByEntry(wordMap, comparator);
//        System.out.println(wordMap);
        Set<Map.Entry<String, Integer>> entries = wordMapLinked.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
    }

    /**
     * 爬去分页数据
     * @param list 数据列表
     * @param pageNo 开始页码
     */
    private void crawlData(List<String> list, int pageNo) {
//        System.out.println(pageNo);
        String url = "https://my.oschina.net/u/3681868/widgets/_space_index_newest_blog?catalogId=0&type=ajax&q=&p=" + pageNo;
        String listContent = HttpUtil.get(url);
//        System.out.println(listContent);
        List<String> listTitle = ReUtil.findAll("<div class=\"ui teal label horizontal\" data-tooltip=\"原创\">原</div>(.*?)</a>", listContent, 1);
//        System.out.println(listTitle.get(0));
//        System.out.println("==========================");
        if (BlankUtil.isBlank(listTitle)) {
            return;
        }
        for (String title : listTitle) {
            list.add(title.trim());
        }
        crawlData(list, pageNo+1);
    }




}
